from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from myapp.models import *
from myapp.serializer import *
from .serializer import *
import json
from decorator.decorator import check_login

# Create your views here.


# 订单表
class OrderSet(ModelViewSet):
    queryset = Order.objects.all()
    serializer_class = OrderSer

    # 获取订单信息
    @check_login
    def list(self, request, *args, **kwargs):
        try:
            uid = request.user.id
            print('uid>>>',uid)

            if not uid:
                return Response({
                    "code":204,
                    "msg": "请先登录！",
                })

            order = Order.objects.filter(user_id=uid).all()
            if not order:
                return Response({
                    "code": 200,
                    "msg": "该用户还没有订单！"
                })
            orderser = OrderSer(order, many=True)

            return Response({
                "code": 200,
                "msg": "获取用户订单成功！",
                "order_list": orderser.data
            })
        except Exception as e:
            print('错误>>>', e)
            return Response({
                "code": 500,
                "msg": "服务器错误！"
            })


# 订单商品表
class OrderGoodsSet(ModelViewSet):
    queryset = OrderGoods.objects.all()
    serializer_class = OrderGoodsSer

    # 获取订单商品信息
    @check_login
    def list(self, request, *args, **kwargs):
        order_id = request.query_params.get('oid')
        print('orid>', order_id)
        if not order_id:
            return Response({
                "code": 204,
                "msg": "没有获取到订单编号！"
            })
        uid = request.user.id
        try:
            if not uid:
                return Response({
                    "code": 200,
                    "msg": "没有获取到用户"
                })
            goods_order = OrderGoods.objects.filter(order_id_id=order_id).all()
            print('goods_order', goods_order)
            if not goods_order:
                return Response({
                    "code": 204,
                    "msg": "该订单0中没有订单商品！"
                })
            goods_order_ser = OrderGoodsSer(goods_order, many=True).data
            goods_order_ser = json.loads(json.dumps(goods_order_ser))

            for i in goods_order_ser:
                dishes = Dishes.objects.filter(id=i["dishes_id"]).first()
                dishes = DishesSer(dishes).data
                dishes = json.loads(json.dumps(dishes))
                i["dishes_list"] = dishes

            return Response({
                "code": 200,
                "msg": "获取订单商品成功！",
                "order_goods": goods_order_ser
            })

        except Exception as e:
            print("错误",e)
            return Response({
                "code": 500,
                "msg": '服务器错误！'
            })


class MerchantOrderAPIView(APIView):
    # 获取订单信息
    @check_login
    def get(self, request):
        try:
            uid = request.user.id
            print('uid>>>',uid)

            if not uid:
                return Response({
                    "code":204,
                    "msg": "请先登录！",
                })

            order = Order.objects.filter(merchant_id=uid).all()
            if not order:
                return Response({
                    "code": 200,
                    "msg": "该用户还没有订单！"
                })
            orderser = OrderSer(order, many=True)

            return Response({
                "code": 200,
                "msg": "获取用户订单成功！",
                "order_list": orderser.data
            })
        except Exception as e:
            print('错误>>>', e)
            return Response({
                "code": 500,
                "msg": "服务器错误！"
            })